from models.product import Product, ProductFactory
from patterns.observer import InventoryWarningObserver
from services.shopping_cart import ShoppingCart
from strategies.discount_strategy import FullReductionStrategy, MemberDiscountStrategy, SeckillDiscountStrategy
from models.order import Order


def initialize_products():
    observer = InventoryWarningObserver()

    product1 = ProductFactory.create_product(1, "商品A", 100.0, 10)
    product2 = ProductFactory.create_product(2, "商品B", 200.0, 3)
    product3 = ProductFactory.create_product(3, "商品C", 150.0, 8)

    product2.add_observer(observer)
    product3.add_observer(observer)

    return [product1, product2, product3]


def main():
    cart = ShoppingCart()
    current_order = None
    products = initialize_products()

    while True:
        print("\n=== 电商购物车系统 ===")
        print("1. 浏览商品")
        print("2. 添加到购物车")
        print("3. 移除商品")
        print("4. 设置折扣策略")
        print("5. 查看购物车")
        print("6. 结账")
        print("7. 查看订单")
        print("8. 退出")

        choice = input("请选择操作：")

        if choice == '1':
            print("\n商品列表：")
            for p in products:
                print(f"ID: {p.product_id}, 名称：{p.name}, 价格：{p.price}, 库存：{p.stock}")

        elif choice == '2':
            product_id = int(input("请输入商品ID："))
            quantity = int(input("请输入数量："))
            product = next((p for p in products if p.product_id == product_id), None)
            if product:
                cart.add_item(product, quantity)
            else:
                print("错误：商品ID不存在。")

        elif choice == '3':
            product_id = int(input("请输入要移除的商品ID："))
            quantity = int(input("请输入要移除的数量："))
            product = next((p for p in products if p.product_id == product_id), None)
            if product:
                cart.remove_item(product, quantity)
            else:
                print("错误：商品ID不存在。")

        elif choice == '4':
            print("\n折扣策略：")
            print("1. 满减")
            print("2. 会员折扣")
            print("3. 秒杀折扣")
            strategy_choice = input("请选择折扣策略：")
            try:
                if strategy_choice == '1':
                    threshold = float(input("请输入满减门槛金额："))
                    reduction = float(input("请输入减去的金额："))
                    cart.set_discount_strategy(FullReductionStrategy(threshold, reduction))
                    print("满减策略已应用。")
                elif strategy_choice == '2':
                    rate = float(input("请输入折扣率（例如0.9表示9折）："))
                    cart.set_discount_strategy(MemberDiscountStrategy(rate))
                    print("会员折扣已应用。")
                elif strategy_choice == '3':
                    rate = float(input("请输入秒杀折扣率（例如0.5表示5折）："))
                    cart.set_discount_strategy(SeckillDiscountStrategy(rate))
                    print("秒杀折扣已应用。")
                else:
                    print("无效的选择。")
            except ValueError:
                print("错误：请输入有效的数字。")

        elif choice == '5':
            print("\n购物车内容：")
            if cart.items:
                for product, qty in cart.items.items():
                    print(f"{product.name} x{qty} - 单价：{product.price}")
                print(f"总计：{cart.calculate_total():.2f}")
            else:
                print("购物车为空。")

        elif choice == '6':
            order = cart.checkout()
            current_order = order if order else current_order

        elif choice == '7':
            if current_order:
                current_order.display()
            else:
                print("暂无订单。")

        elif choice == '8':
            print("感谢使用，再见！")
            break

        else:
            print("无效的选择，请重新输入。")


if __name__ == "__main__":
    main()

